//
//  JMJTabBarController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/11/30.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "JMJTabBarController.h"
#import "JMJRootNavigationController.h"
#import "HomePageViewController.h"
#import "FollowViewController.h"
#import "ChannelViewController.h"
#import "PersonalBiddingViewController.h"
#import "UserCenterViewController.h"
#import "LoginViewController.h"

@interface JMJTabBarController ()

@end

@implementation JMJTabBarController

- (instancetype)init{
    self = [super init];
    if (self) {
        [self customViewControllers];
        
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"login"];
        
        self.delegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)customViewControllers{

    
    HomePageViewController *hvc = [[HomePageViewController alloc] init];
    JMJRootNavigationController *nc = [[JMJRootNavigationController alloc] initWithRootViewController:hvc];
    FollowViewController *fvc = [[FollowViewController alloc] init];
    JMJRootNavigationController *nc2 = [[JMJRootNavigationController alloc] initWithRootViewController:fvc];
    PersonalBiddingViewController *pvc =[[PersonalBiddingViewController alloc] init];
    JMJRootNavigationController *nc3 = [[JMJRootNavigationController alloc] initWithRootViewController:pvc];
    UserCenterViewController *uvc = [[UserCenterViewController alloc] init];
    JMJRootNavigationController *nc4 = [[JMJRootNavigationController alloc] initWithRootViewController:uvc];
    
    self.viewControllers = @[nc, nc2, nc3, nc4];
    
    self.tabBar.tintColor = [UIColor whiteColor];
    
    [self customNavBarWithViewController:fvc];
    [self customNavBarWithViewController:pvc];
    [self customNavBarWithViewController:uvc];
    
    UIImageView *view = [[UIImageView alloc] initWithFrame:self.tabBar.bounds];
    UIImage *image = [UIImage imageNamed:@"TAB.9.png"];
    view.contentMode = UIViewContentModeScaleToFill;
    view.image = image;
    [self.tabBar insertSubview:view atIndex:0];
}

- (void)customNavBarWithViewController:(UIViewController *)vc{
    
    if ([vc isKindOfClass:[FollowViewController class]]) {
        vc.title = @"关注";
        UIImage *normalImage = [[UIImage imageNamed:@"关注1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *selectedImage = [[UIImage imageNamed:@"关注.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.navigationController.tabBarItem.image = normalImage;
        vc.navigationController.tabBarItem.selectedImage = selectedImage;
    }else if([vc isKindOfClass:[PersonalBiddingViewController class]]){
        vc.title = @"个人竞价室";
        UIImage *normalImage = [[UIImage imageNamed:@"个人竞价室1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *selectedImage = [[UIImage imageNamed:@"个人竞价室.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.navigationController.tabBarItem.image = normalImage;
        vc.navigationController.tabBarItem.selectedImage = selectedImage;
    }else if([vc isKindOfClass:[UserCenterViewController class]]){
        vc.title = @"用户中心";
        UIImage *normalImage = [[UIImage imageNamed:@"用户中心1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *selectedImage = [[UIImage imageNamed:@"用户中心.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.navigationController.tabBarItem.image = normalImage;
        vc.navigationController.tabBarItem.selectedImage = selectedImage;
    }
    
    
}

//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//    if (![viewController.navigationController isKindOfClass:[JMJRootNavigationController class]]) {
//        NSLog(@"awesome ! %ld %@", viewController.tabBarController.selectedIndex, [viewController class]);
//        if (viewController.tabBarController.selectedIndex != 0 && ![[NSUserDefaults standardUserDefaults] boolForKey:@"login"]) {
//            LoginViewController *lvc = [[LoginViewController alloc] init];
//            JMJRootNavigationController *nc = [[JMJRootNavigationController alloc] initWithRootViewController:lvc];
//            [viewController presentViewController:nc animated:YES completion:^{
//                NSLog(@"done");
//            }];
//        }
//    }
//}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"login"]) {
        if (!tabBarController.selectedIndex) {
            LoginViewController *lvc = [[LoginViewController alloc] init];
            JMJRootNavigationController *nc = tabBarController.selectedViewController;
            lvc.selectedIndex = viewController.tabBarController.selectedIndex;
            JMJRootNavigationController *rnc = (JMJRootNavigationController *)viewController;
            NSLog(@"%@ --------%@",rnc, rnc.viewControllers);
            [nc pushViewController:lvc animated:NO];
            return NO;
        }
    }
    return YES;
}

@end
